package org.qiaice.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.qiaice.context.BaseContext;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class AutoFillFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        strictInsertFill(metaObject, "createTime", LocalDateTime.class, now);
        strictInsertFill(metaObject, "updateTime", LocalDateTime.class, now);
        strictInsertFill(metaObject, "status", Integer.class, 1);
        Long id = BaseContext.getContext().get("id", Long.class);
        strictInsertFill(metaObject, "createUser", Long.class, id);
        strictInsertFill(metaObject, "updateUser", Long.class, id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = BaseContext.getContext().get("id", Long.class);
        strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        strictUpdateFill(metaObject, "updateUser", Long.class, id);
    }
}
